package com.itzyf.pokemondata.activity

import android.text.TextUtils
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.itzyf.pokemondata.R
import com.itzyf.pokemondata.bean.DbFestivalEnvoyDo
import com.itzyf.pokemondata.utils.OnRefreshListener
import com.itzyf.pokemondata.utils.setOnRefreshListener
import com.tydic.baselibrary.activity.BaseActivity
import kotlinx.android.synthetic.main.activity_list.*
import org.litepal.LitePal

/**
 * @author 依风听雨
 * @version 创建时间：2019/04/29 19:36
 */
class FestivalEnvoyActivity : BaseActivity() {
    var mDatas: MutableList<QADo?> = ArrayList()
    private lateinit var mAdapter: QuickAdapter
    var index = 0

    override fun handlerActivity() {
        mAdapter = QuickAdapter(R.layout.item_q_a, mDatas)
        rv_list.adapter = mAdapter

        rv_list.layoutManager = LinearLayoutManager(this)
        mAdapter.setOnLoadMoreListener({ queryDatas(index++) }, rv_list)

        pull_to_refresh.setOnRefreshListener(object : OnRefreshListener {
            override fun onRefresh() {
                index = 0
                queryDatas(index)
            }
        })
        mSearchView.hint = "请输入问题关键字"
        mBtnSearch.setOnClickListener {
            val query = mSearchView.text.toString()
            queryDatas(query = query)
        }
        mSearchView.setOnEditorActionListener(TextView.OnEditorActionListener { _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                val query = mSearchView.text.toString()
                queryDatas(query = query)
                return@OnEditorActionListener true
            }
            false
        })
        queryDatas()
    }

    /**
     * 查询数据
     */
    fun queryDatas(index: Int = 0, pageSize: Int = 20, query: String = "") {
        var where = ""
        if (!TextUtils.isEmpty(query)) {
            where = "question like '%$query%'"
        }
        val dataList =
            LitePal
                .limit(pageSize)
                .offset(index * pageSize)
                .order("createdAt desc")
                .where(where)
                .find(DbFestivalEnvoyDo::class.java)

        if (index == 0) {
            mDatas.clear()
            pull_to_refresh.finishRefresh()
        }

        for (obj in dataList) {
            mDatas.add(QADo(obj.question, obj.answer))
        }

        if (dataList.size < pageSize) {
            mAdapter.loadMoreEnd()
        } else {
            mAdapter.loadMoreComplete()
        }
        mAdapter.notifyDataSetChanged()
    }

    internal inner class QuickAdapter(layoutResId: Int, data: List<QADo?>) :
        BaseQuickAdapter<QADo, BaseViewHolder>(layoutResId, data) {

        override fun convert(helper: BaseViewHolder, item: QADo?) {
            helper.setText(R.id.tvQuestion, "Q:${item?.question}")
            helper.setText(R.id.tvAnswer,"A:${item?.answer}")
        }
    }


    override fun getContentViewId() = R.layout.activity_list

    override fun getToolbarTitle() = "节日使者Q&A"

    data class QADo(
        val question: String,
        val answer: String
    )
}